package lsg;

/**
 * Exercise 1		Simulationstechnink WS2005/2006
 *
 * DES01 class
 * Mainclass reading the arguments and performing the simulation.
 *
 * @author Alexander Klein
 * @version 1.0.0 
 * @since 2005-10-22
 */
public class  DES01
{
	/**
	  * Main executing fuction
	  *@param args consoleparameters
	 */
	public static void main(String[] args) 
	{
		/**
		 * Checks if the number of arguments is correct and uses them for initialization
		 * of the simulation
		 */
		if (args.length != 3)
		{
			System.out.println("Three arguments needed to start simulation!");
			System.out.println("java DES01 [interArrivalTime] [serviceCompletion] [simulationDuration]");
			System.exit (-1);
		}
		
		else
		{
			// Initialize SimState with the given arguments
			SimEvent.s = new SimState ( Integer.parseInt(args [0]), Integer.parseInt(args [1]), Integer.parseInt(args [2]));
		}
		
		//Insert the first and the last event
		SimEvent.s.ec.insert (new CustomerArrival (0));
		SimEvent.s.ec.insert (new SimulationTermination(SimEvent.s.simulationDuration));
		
		/**
		 * Simulation is done here
		 */
		while (SimEvent.s.stop != true)
		{	
			// Get the next SimEvent from the EventChain
			SimEvent e = SimEvent.s.ec.removeOldestEvent();
			
			if (e != null)
			{
				if (SimEvent.s.now > e.value)
				{
					System.out.println("_____________________________________________________");
					System.out.println("ERROR");
					System.out.println("Event_Time" + e.value);
					System.out.println("System_Time" + SimEvent.s.now);
					System.out.println("EVENT SHOULD BE ALREADY EXECUTED IN THE PAST"); 
					System.out.println("_____________________________________________________");
				}
				
				SimEvent.s.now = e.value;
				
				e.process();
			}
			else
			{
				System.out.println("EventChain is empty! Simulation abort!");
				SimEvent.s.stop = true;
			}
			
			// Updating the min and the max attribute of the SimState
			SimEvent.s.min = (SimEvent.s.queueSize < SimEvent.s.min ? SimEvent.s.queueSize:SimEvent.s.min);
			SimEvent.s.max = (SimEvent.s.queueSize > SimEvent.s.max ? SimEvent.s.queueSize:SimEvent.s.max);
		}
		
		
		// reporting
		System.out.println("E[inter-arrival time] = " + SimEvent.s.interArrivalTime);
		System.out.println("E[service completion time] = " + SimEvent.s.serviceCompletionTime);
		System.out.println("simulation duration = " + SimEvent.s.simulationDuration);
		System.out.println("minimum queue length = " + SimEvent.s.min);
		System.out.println("maximum queue length = " + SimEvent.s.max);
	}
}
